// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_gray");
    var top = require("../top/top_white");
    var retrievePasswordUuid;
    var mobile;
    var authenticode;
    var tools = require("tools");
    var wait = 60;
    var flag = true;
    var check = true;
    var init = function(){
    	footer.init();
    	top.init("找回密码");
    	$("#head").removeClass("none");
    	$("#main").removeClass("none");
    	$("#footer").removeClass("none");
    	router.init();
		initEvent(); 
    };
    
	var initEvent = function(){
		
		/*失去焦点时验证是否手机号码*/
		$("#main").on("blur", "#phone input", function() {
			var phoneNum = $("#phone input").val();
			if(phoneNum == null || phoneNum ==""){
				$("#errinfo1").removeClass("none");
				$("#errinfo1").html("请输入手机号码！");
			}else if(!validatePhoneNum(phoneNum)) {
				$("#errinfo1").removeClass("none");
				$("#errinfo1").html("请输入正确的手机号码！");
				flag = false;
			}else{
				$("#errinfo1").addClass("none");
				flag = true;
			}
		});
		
		/*验证是否已输入验证码*/
		$("#main").on("blur", "#code input", function() {
			var code = $("#code input").val();
			if(code == null || code =="") {
				$("#errinfo2").removeClass("none");
				$("#errinfo2").html("请输入验证码！");
				flag = false;
			}else if(code.length != 6){
				$("#errinfo2").removeClass("none");
				$("#errinfo2").html("请输入正确的验证码！");
				flag = false;
			}else{
				$("#errinfo2").addClass("none");
				flag = true;
			}
		});
		
		tools.geetest($("#code a"), $("#popup-captcha"), function(){
			var phoneNum = $("#phone input").val().trim();
			if (phoneNum == "") {
				$("#errinfo1").removeClass("none");
				$("#errinfo1").html("请输入手机号码！");
				return;
			} else if (!validatePhoneNum(phoneNum)) {
				$("#errinfo1").removeClass("none");
				$("#errinfo1").html("请输入正确的手机号码！");
				return;
			}
			if(check && wait==60){
				check = false;
				retrieveEnterprisePassword();
			}
			if(wait>0){
//				$("#code a").unbind("click");
			}
		}, function(){});
		
		$("#main").on("click", "#next a", function() {
			if(flag && $("#accept input").prop("checked")){
				retrieve();
			}else{
				$("#errinfo3").removeClass("none");
				$("#errinfo3").html("请仔细阅读免责条款！");
				flag = false;
			}
		});
    };
    

	 //找回密码
    var retrieve = function() {
    	authenticode = $("#code input").val();
    	mobile = $("#phone input").val();
    	base.fnAjax({
            action: "validateEnterpriseChangePasswordUuid.json",
            nocode: true,
            oData: {
                mobile: mobile,
                retrievePasswordUuid: retrievePasswordUuid,
                authenticode: authenticode,
                nocode: true
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
               	 	router.route("retrievePassword/retrieve_setPsw.html?mobile="+mobile+"&authenticode="+authenticode+"&retrievePasswordUuid="+retrievePasswordUuid);
                } else {
               	 	$("#errinfo3").removeClass("none");
					$("#errinfo3").html(data.message);
					flag = false;
                }
            }
        });
    }
    
    //获取验证码
	var retrieveEnterprisePassword = function(){
		mobile = $("#phone input").val();
        base.fnAjax({
            action: "retrieveEnterprisePassword.json",
            nocode: true,
            oData: {
                mobile: mobile
            }, //传递参数
            fnSuccess: function(data) {
				check = true;
                if (data.status == 200) {
					time();
					showOk();
					retrievePasswordUuid = data.datas[0].retrievePasswordUuid;
                } else {
					$("#errinfo1").removeClass("none");
					$("#errinfo1").html("该手机号码尚未注册！");
                }
            }
        });
	}
	
	//验证码发送成功提示
	var showOk = function(){
		$("#ok").removeClass("none");
		$("#ok").html("验证码发送成功，请留意！");
		setTimeout(function() {
			$("#ok").addClass("none");
		},
		3000)
	}
    
     //限制60秒内不能重复获取验证码
    var time = function() {
    	if (wait == 0) {
			$("#code a").removeAttr("disabled");
			$("#code a").html("获取验证码");
			wait = 60;
		} else {
			$("#code a").unbind("click");
			$("#code a").attr("disabled", "true");
			$("#code a").html("重新发送(" + wait + ")");
			wait--;
			setTimeout(function() {
				time()
			},
			1000)
		}
    }
    
    //验证手机号码是否正确
    var validatePhoneNum = function(str) {
    	var regMobile = /^0?1[3|4|5|8][0-9]\d{8}$/ //手机
    	if(regMobile.test(str)) {
    		return true;
    	}else {
    		return false;
    	}
    }

    return {
        init:init
    };
});